package com.feilong.edgeAI.remind.room.ui;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.feilong.edgeAI.R;
import com.feilong.edgeAI.remind.room.adapter.RemindListAdapter;
import com.feilong.edgeAI.remind.room.bean.RemindBean;
import com.feilong.edgeAI.remind.room.db.RemindDatabase;
import com.feilong.edgeAI.utils.TextUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CycleFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CycleFragment extends Fragment implements EditDialog.OnRefreshDataListener {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;
    private View view;
    private RecyclerView recyclerView;
    private RemindListAdapter mAdapter;
    private List<RemindBean> remindBeanList = new ArrayList<>();
    private RadioGroup rg_remind;
    private int notifyId = 1;

    public CycleFragment() {
        // Required empty public constructor
    }

    public static CycleFragment newInstance(String param1, String param2) {
        CycleFragment fragment = new CycleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view = inflater.inflate(R.layout.fragment_cycle, container, false);
        recyclerView = view.findViewById(R.id.recyclerView_cycle);
        rg_remind = view.findViewById(R.id.rg_remind);

        rg_remind.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
                notifyId = 1;
                switch (checkedRadioButtonId){
                    case R.id.rb_day:
                        notifyId = 1;
                        break;

                    case R.id.rb_week:
                        notifyId = 2;
                        break;

                    case R.id.rb_month:
                        notifyId = 3;
                        break;

                    case R.id.rb_year:
                        notifyId = 4;
                        break;
                }

                notifyRemindData(getActivity(),notifyId);
            }
        });

        recyclerView.setLayoutManager(new GridLayoutManager(getActivity().getApplicationContext(), 1));
        remindBeanList = RemindDatabase.getDefault(getActivity().getApplicationContext()).getRemindDao().loadRemindByModes("1");
        mAdapter = new RemindListAdapter(remindBeanList);
        notifyRemindData(getActivity(),1);

        return view;
    }

    public void notifyRemindData(Context context,int i) {

        String strIn = String.valueOf (i + 1);
        remindBeanList = RemindDatabase.getDefault(context).getRemindDao().loadRemindByModes(strIn);
        RemindListAdapter mAdapter = new RemindListAdapter(remindBeanList);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new RemindListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RemindBean remindBean) {
                Bundle mBundle = new Bundle();
                mBundle.putParcelable("RemindBean", remindBean);
                EditDialog mDialog = new EditDialog();
                mDialog.show(getActivity(), mBundle, "edit");
                mDialog.setOnRefreshDataListener(new EditDialog.OnRefreshDataListener() {
                    @Override
                    public void onRefresh() {

                        notifyRemindData(getActivity(),notifyId);
                    }
                });
            }
        });
    }


    @Override
    public void onRefresh() {

    }
}